#include <mqueue.h>
#include <fcntl.h>
#include <stdio.h>




int main(int argc, char const *argv[])
{
    // 0 先清除已存在的队列
    mq_unlink("/mq_test");
    //1  创建并打开队列
    struct  mq_attr attr;
    attr.mq_maxmsg=10;
    attr.mq_msgsize=256;

    mqd_t mqd= mq_open("/mq_test",O_CREAT | O_RDWR, 0664, &attr  );

    //2  发送数据 
    int ret=  mq_send(mqd, "hello world", 11, 0);
    if(ret==-1)
    {
        perror("mq_send");
        return -1;
    }

    //3  接收数据 
    char buf[256];
    ret= mq_receive(mqd, buf, 256, 0);
    if(ret==-1)
    {
        perror("mq_receive");
        return -1;
    }

    printf("receive data: %.*s\n",11, buf);

    //4  关闭队列 /销毁队列
    mq_close(mqd);
    mq_unlink("/mq_test");
    
    return 0;
}
